package com.lphj.datainterface.handle;

import com.lphj.datainterface.component.Config.ReadConfig;
import com.lphj.datainterface.component.Config.ReadCumOrderForMCConfig;
import com.lphj.datainterface.entity.CumOrderDtl;
import com.lphj.datainterface.entity.TGoods;
import com.lphj.datainterface.service.CumOrderDtlService;
import com.lphj.datainterface.service.TGoodsService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class CumOrderDtlForMCHandle {

    @Autowired
    TGoodsService tGoodsService;

    @Autowired
    CumOrderDtlService cumOrderDtlService;

    @Autowired
    CumOrderListHandle cumOrderListHandle;

    @Autowired
    ReadCumOrderForMCConfig readCumOrderForMCConfig;
    /**
     *
     * @param sheet 订单商品明细表
     * @param orderId 观麦订单编号
     * @param paperNO ERP订单编号
     * @return
     */
    public Map<String,Object> insertCumOrderDtl(Sheet sheet, String orderId,
                                                String paperNO,String cumNO,String storeNO,Date orderDate,Date deliveryDate) {
        Map<String,Object> info = new HashMap<String,Object>();
        int itemMapping = 0;
        int countMapping = 0;
        //获取首行标题
        List<String> firstRowTitleMapping = new ArrayList<String>();
        Row firstRowMapping = sheet.getRow(0);
        boolean isNotNullMapping = true;
        int indexMapping = 0;
        //获取首行标题
        while(isNotNullMapping) {
            Cell cellMapping = firstRowMapping.getCell(indexMapping);
            if(cellMapping != null) {
                firstRowTitleMapping.add(cellMapping.getStringCellValue());
            } else {
                isNotNullMapping = false;
            }
            indexMapping ++;
        }
        //订单编号
        int romc_mcddbh = 0;
        //SSUID
        int romc_mcssuid = 0;
        //实收金额
        int romc_mcssje = 0;
        //实收量
        int romc_mcssl = 0;
        //商品单位
        int romc_mcunit = 0;
        for(int j=0;j<firstRowTitleMapping.size();j++) {
            if(firstRowTitleMapping.get(j).equals(readCumOrderForMCConfig.getMcddbh())) {
                romc_mcddbh = j;
            } else if(firstRowTitleMapping.get(j).equals(readCumOrderForMCConfig.getMcssuid())) {
                romc_mcssuid = j;
            } else if(firstRowTitleMapping.get(j).equals(readCumOrderForMCConfig.getMcssje())) {
                romc_mcssje = j;
            } else if(firstRowTitleMapping.get(j).equals(readCumOrderForMCConfig.getMcssl())) {
                romc_mcssl = j;
            } else if(firstRowTitleMapping.get(j).equals(readCumOrderForMCConfig.getMcunit())) {
                romc_mcunit = j;
            }
        }

        for (int k = 1; k <= sheet.getLastRowNum(); k++) {
            //订单项次
            itemMapping ++;
            //读取第r行
            Row rowMapping = sheet.getRow(k);
            if(rowMapping == null) {
                break;
            }
            //获取订单号
            Cell cellMapping = rowMapping.getCell(romc_mcddbh);
            cellMapping.setCellType(CellType.STRING);
            //查找单号
            if(cellMapping.getStringCellValue().equals(orderId)) {
                //商品美菜编码
                cellMapping = rowMapping.getCell(romc_mcssuid);
                cellMapping.setCellType(CellType.STRING);
                String spuid = cellMapping.getStringCellValue();
                //获取商品
                TGoods tgoods = tGoodsService.getGoodsBySGoodsNO3(spuid);
                if(tgoods != null) {
                    //创建数据对象
                    CumOrderDtl cumOrderDtl = new CumOrderDtl();
                    //ERP商品ID
                    cumOrderDtl.setGoodsID(tgoods.getGoodsID());
                    //最后修改时间
                    cumOrderDtl.setLastUpdateTime(new Date());
                    //设置ERP订单编号
                    cumOrderDtl.setPaperNO(paperNO);
                    //设置项次
                    cumOrderDtl.setItem(itemMapping);
                    //设置商品名称
                    cumOrderDtl.setGoodsDesc(tgoods.getGoodsDesc());
                    //设置单位
                    cellMapping = rowMapping.getCell(romc_mcunit);
                    //预定单位
                    cumOrderDtl.setUnit(cellMapping.getStringCellValue());
                    //实订单位
                    cumOrderDtl.setOrdUnit(cellMapping.getStringCellValue());
                    //生鲜商品如果单位不是斤，则与斤的换算关系，农副产品填1
                    cumOrderDtl.setFactor(1.00);
                    //订单数量
                    cellMapping = rowMapping.getCell(romc_mcssl);
                    //预订数量
                    cellMapping.setCellType(CellType.STRING);
                    cumOrderDtl.setQty(Double.parseDouble(cellMapping.getStringCellValue()));
                    //实订数量
                    cumOrderDtl.setOrdQty(Double.parseDouble(cellMapping.getStringCellValue()));
                    //商品总售价
                    cellMapping = rowMapping.getCell(romc_mcssje);
                    cellMapping.setCellType(CellType.STRING);
                    double t = Double.parseDouble(cellMapping.getStringCellValue());
                    //售价
                    cumOrderDtl.setSalePrice(t/cumOrderDtl.getOrdQty());
                    //是否已汇总 0=否 1=是
                    cumOrderDtl.setTag(0);
                    countMapping += cumOrderDtlService.addCumOrderDtl(cumOrderDtl);
                    if(cumNO != null && !cumNO.equals("")) {
                        cumOrderListHandle.insertCumOrderList(cumOrderDtl,tgoods,cumNO,storeNO,orderDate,deliveryDate);
                    } else {
                        System.out.println("is null");
                    }
                } else if (tgoods == null){
                    info.put(cellMapping.getStringCellValue(),cellMapping.getStringCellValue() + "----商品不存在，请在ERP添加或更新该商品！");
                }
                info.put("success",countMapping);
            }

        }
        return info;
    }
}
